#include "DungeonsScene.h"
#include "../GlobalLayer.h"
#include "../../../model/vo/ViewVO.h"
#include "../../../model/proxy/DungeonsProxy.h"
#include "../../../model/static/StaticDungeons.h"
#include "../UserInfoLoader.h"
#include "../MainMenuLoader.h"
#include "../TaskDescLoader.h"
#include "../TaskListLoader.h"
#include "../../../model/proxy/FightProxy.h"
#include "../../../utils/LocalString.h"
#include "../NotifyDefine.h"

DungeonsScene::DungeonsScene(void)
	:mTaskDesc(NULL),mTaskBoss(NULL),mTaskList(NULL)
{
	
}


DungeonsScene::~DungeonsScene(void)
{
}

bool DungeonsScene::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
	return false;
}

void DungeonsScene::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
}

void DungeonsScene::onEnter()
{
	State::onEnter();
	CCArray* nameList = CCArray::create(ccs(kNCRegist),ccs(kNCDungeonExplore),ccs(kNCSellEquipage),ccs(kNCChallengePlayer),ccs(kNCGetSelfInfo),NULL);
	RegisterObservers(this, nameList, callfuncO_selector(DungeonsScene::_onNotification));
	refresh();
}

void DungeonsScene::onExit()
{
	RemoveObserver(this);
	State::onExit();
}

bool DungeonsScene::init()
{
	State::init();
	mTaskDesc = TaskDesc::create(this);
	mTaskDesc->setPosition(0, 238);
	mTaskBoss = TaskBoss::create(this);
	mTaskBoss->setPosition(0, 0);
	mTaskList = TaskList::create(this);
	mTaskList->setPosition(0, 65);
	this->addChild(mTaskDesc);
	this->addChild(mTaskList);
	this->addChild(mTaskBoss);
	return true;
}

void DungeonsScene::refresh()
{
	TaskStatic* taskInfo = DungeonsProxy::getSingletonPtr()->getCurTaskStatic();
	mTaskDesc->retain();
	mTaskList->retain();
	mTaskBoss->retain();
	mTaskDesc->removeFromParentAndCleanup(false);
	mTaskList->removeFromParentAndCleanup(false);
	mTaskBoss->removeFromParentAndCleanup(false);
	if (taskInfo->bossID == -1)
	{
		mTaskDesc->refresh();
		this->addChild(mTaskDesc);
		this->addChild(mTaskList);
	}
	else
	{
		mTaskBoss->refresh();
		this->addChild(mTaskBoss);
	}
}


void DungeonsScene::_onNotification( CCObject* object )
{
	CCLog("DungeonsScene::_onNotification[%x]", this);

	NotificationObserver* notification = (NotificationObserver*)object;
	std::string name = std::string(notification->getName());
	if (name == kNCDungeonExplore)
	{
		int win = FightProxy::getSingletonPtr()->battleInfo.win;
		if (win == 1)
		{
			Framework::getSingletonPtr()->changeState("FightSucceedScene");
		}
		else if(win == 2 )
		{
			Framework::getSingletonPtr()->changeState("FightFailedScene");
		}
		else
		{
			playAwardAnim();
			Award& award = FightProxy::getSingletonPtr()->awardInfo;
			if (award.equipList.size() > 0)
				Framework::getSingletonPtr()->popup("AwardEquipDialog", award.equipList[0]);
			this->refresh();
			/*if (FightProxy::getSingletonPtr()->awardInfo.exp > 0 || 
			FightProxy::getSingletonPtr()->awardInfo.coin > 0) 
			Framework::getSingletonPtr()->popup("SearchResultDialog", NULL);*/
		}
		PostNotification(kVCLevelUp, NULL);
	}
}

void DungeonsScene::playAwardAnim()
{
	Award& award = FightProxy::getSingletonPtr()->awardInfo;
	if (award.exp > 0 || award.coin > 0)
	{
		FloatText::getSingletonPtr()->playAnim(fls("get_award",award.exp,award.coin));
	}
}

